<template>
  <van-icon color="yellow" name="star" v-if="isCollected" @click="doCollect" />
  <van-icon color="#777" name="star-o" v-else @click="doCollect" />
</template>

<script>
import { addCollect, delCollect } from '@/api/user'
export default {
  name: 'isCollected',
  model: {
    prop: 'isCollected',
    event: 'change'
  },
  props: {
    // 收藏状态
    isCollected: {
      required: true
    },
    // 文章id
    artId: {
      required: true
    }
  },

  data () {
    return {}
  },

  mounted () {},

  methods: {
    async doCollect () {
      try {
        if (this.isCollected) {
          await delCollect(this.artId)
        } else {
          await addCollect(this.artId)
        }
        this.$emit('change', !this.isCollected)
        this.$toast.success('操作成功')
      } catch (error) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
